package com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.config;

import com.tbit.uqbike.webmanager.business.maintain_performance.constant.PerformanceBizType;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.PerformanceRuleGroup;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.BizPerformanceConfig;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.BizRuleGroup;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.group.*;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 调度绩效配置的业务对象
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/24
 */
@Data
public class DispatchPerformanceConfig implements BizPerformanceConfig {
    /**
     * 区域id
     */
    private Integer accountId;

    /**
     * 基础绩效
     */
    private BasicPerformanceGroup basic;
    /**
     * 产生订单时段绩效
     */
    //private IntervalBasedGroup intervalBased;
    /**
     * 产生订单时效绩效
     */
    //private ValidityBasedGroup validityBased;
    /**
     * 产生订单时段与时效组合绩效
     */
    private IntervalAndValidityBasedGroup intervalAndValidityBased;
    /**
     * 异常车辆处理绩效
     */
    private AbnormalSolvedGroup abnormalSolved;
    /**
     * 闲置时长绩效
     */
    private UnusedTimeBasedGroup unusedTimeBased;
    /**
     * 调度距离绩效
     */
    private MoveDistanceBasedGroup distanceBased;
    /**
     * 消除车辆标签绩效
     */
    private TagSolvedGroup tagSolved;


    public static DispatchPerformanceConfig fromRaw(List<PerformanceRuleGroup> groups, Integer accountId) {
        DispatchPerformanceConfig config = new DispatchPerformanceConfig();
        config.accountId = accountId;
        for (PerformanceRuleGroup group : groups) {
            switch (group.getRuleType()) {
                case BASIC:
                    config.basic = new BasicPerformanceGroup();
                    config.basic.initFromRaw(group);
                    break;
                case INTERVAL_AND_VALIDITY:
                    config.intervalAndValidityBased = new IntervalAndValidityBasedGroup();
                    config.intervalAndValidityBased.initFromRaw(group);
                    break;
                case MACHINE_ABNORMAL:
                    config.abnormalSolved = new AbnormalSolvedGroup();
                    config.abnormalSolved.initFromRaw(group);
                    break;
                case UNUSED_TIME:
                    config.unusedTimeBased = new UnusedTimeBasedGroup();
                    config.unusedTimeBased.initFromRaw(group);
                    break;
                case DISTANCE:
                    config.distanceBased = new MoveDistanceBasedGroup();
                    config.distanceBased.initFromRaw(group);
                    break;
                case MACHINE_TAG:
                    config.tagSolved = new TagSolvedGroup();
                    config.tagSolved.initFromRaw(group);
                    break;
            }
        }
        return config;
    }

    @Override
    public List<PerformanceRuleGroup> toRaw() {
        BizRuleGroup[] groups = new BizRuleGroup[]{
                basic,
                intervalAndValidityBased,
                abnormalSolved,
                unusedTimeBased,
                distanceBased,
                tagSolved
        };

        List<PerformanceRuleGroup> rawGroups = new ArrayList<>(7);
        for (BizRuleGroup group : groups) {
            if (group != null) {
                rawGroups.add(group.toRaw(PerformanceBizType.DISPATCH, accountId));
            }
        }

        return rawGroups;
    }

    @Override
    public void initDefaultIfNull() {
        if (basic == null) {
            basic = new BasicPerformanceGroup();
            basic.initDefault();
        }
        if (intervalAndValidityBased == null) {
            intervalAndValidityBased = new IntervalAndValidityBasedGroup();
            intervalAndValidityBased.initDefault();
        }
        if (abnormalSolved == null) {
            abnormalSolved = new AbnormalSolvedGroup();
            abnormalSolved.initDefault();
        }
        if (unusedTimeBased == null) {
            unusedTimeBased = new UnusedTimeBasedGroup();
            unusedTimeBased.initDefault();
        }
        if (distanceBased == null) {
            distanceBased = new MoveDistanceBasedGroup();
            distanceBased.initDefault();
        }
        if (tagSolved == null) {
            tagSolved = new TagSolvedGroup();
            tagSolved.initDefault();
        }
    }
}
